package org.lojzes.minstruts.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ClassFull {
	private static Log log = LogFactory.getLog(ClassFull.class);
	private static ThreadLocal<ClassFull> localData = new ThreadLocal<ClassFull>();
	private static ClassFull instance = null;

	private ClassFull() {
	}

	public static ClassFull getInstance() {

		if (null == localData.get()) {
			instance = new ClassFull();
			localData.set(instance);
		}
		return localData.get();
	}
    /**
     * 调用action 方法，返回视图
     * @param actionType
     * @param method
     * @param request
     * @param response
     * @return
     */
	public String fullAction(String actionType,String method,HttpServletRequest request,HttpServletResponse response) {
		Object action = null;
		String forward="";
		try {
			Class<?> classz = Class.forName(actionType);
			action = classz.newInstance();
			Field[] fields = classz.getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				field.set(action, request.getParameter(field.getName()));
				field.setAccessible(false);
			}
			/**
			 * 约定：action 方法的参数个数为2;类型必须为 HttpServletRequest 和 HttpServletResponse
			 */
			Method met= classz.getMethod(method,HttpServletRequest.class,HttpServletResponse.class);
			forward = (String)met.invoke(action,request,response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("===>>严重：" + e);
		}
		return forward;
	}
}
